﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz.Report;
using Newtonsoft.Json;
using CRMBiz;
using CRMWeb.Utilities.Context;
using System.IO;
namespace CRMWeb.WebServices.Report
{
    /// <summary>
    /// Summary description for Report
    /// </summary>
    public class Report : BaseHandler
    {
        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        //user defined function


        public void GetLoanPurposeReportByDate(string selectedMonth, string selectedYear)
        {
            try
            {
                DataTable loanPurposeReport = ReportHandler.GetLoanPurposeReportByDate(selectedMonth, selectedYear);
                string strJson = JsonConvert.SerializeObject(loanPurposeReport);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }



        public void GetLoanPurposeReportByPeriod(string fromDate, string toDate)
        {
            try
            {
                DateTime startDate = Convert.ToDateTime(fromDate);
                DateTime finishDate = Convert.ToDateTime(toDate).AddDays(1);
                DataTable loanPurposeReport = ReportHandler.GetLoanPurposeReportByPeriod(startDate, finishDate);
                string strJson = JsonConvert.SerializeObject(loanPurposeReport);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        //Get Client State Analysis Report by Date
        public void GetClientStateReportByDate(string selectedMonth, string selectedYear)
        {
            try
            {
                DataTable statePurposeReport = ReportHandler.GetStateReportByDate(selectedMonth, selectedYear);
                string strJson = JsonConvert.SerializeObject(statePurposeReport);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        //Get Client State Analysis Report by Period
        public void GetClientStateReportByPeriod(string fromDate, string toDate)
        {
            try
            {
                string strFrm = "";
                string strTo = "";
                if (fromDate != "")
                {
                    string[] sa = fromDate.Split('/');
                    strFrm = sa[2] + "-" + sa[1] + "-" + sa[0];
                }
                if (toDate != "")
                {
                    string[] ea = toDate.Split('/');
                    strTo = ea[2] + "-" + ea[1] + "-" + ea[0];
                }

                DataTable statePurposeReport = ReportHandler.GetStateReportByPeriod(strFrm, strTo);
                string strJson = JsonConvert.SerializeObject(statePurposeReport);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        // Get Test2014 Report
        public void GetTestReport2014(string strState, string comesfrom)
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataTable dt = ReportHandler.GetTest2014Report(strState, comesfrom);
                var total = dt.Rows.Count;
                
                if (total > 0)
                    dt = dt.AsEnumerable().Skip((pageIndex - 1) * pageSize).Take(pageSize).CopyToDataTable();
               
                string strJson = JsonConvert.SerializeObject(dt);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";
                context.Response.Write(strJson);

            }
            catch (Exception ex)
            {
                context.Response.Write("[]");
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

    }
}